package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.biz.entity.WmMdWarehouse;
import cn.stylefeng.guns.modular.biz.entity.WmMdZone;
import cn.stylefeng.guns.modular.biz.mapper.WmMdWarehouseMapper;
import cn.stylefeng.guns.modular.biz.mapper.WmMdZoneMapper;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.WmMdWarehouseManage;
import cn.stylefeng.guns.modular.biz.enums.WmMdWarehouseManageExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.WmMdWarehouseManageMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.WmMdWarehouseManageRequest;
import cn.stylefeng.guns.modular.biz.service.WmMdWarehouseManageService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 煤场管理代码业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/13 16:37
 */
@Service
public class WmMdWarehouseManageServiceImpl extends ServiceImpl<WmMdWarehouseManageMapper, WmMdWarehouseManage> implements WmMdWarehouseManageService {

    @Autowired
    private WmMdWarehouseMapper wmMdWarehouseMapper;
    @Autowired
    private WmMdZoneMapper wmMdZoneMapper;

	@Override
    public void add(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        WmMdWarehouseManage wmMdWarehouseManage = new WmMdWarehouseManage();
        BeanUtil.copyProperties(wmMdWarehouseManageRequest, wmMdWarehouseManage);

        String  siteCode=wmMdWarehouseManage.getManagerSiteCode();//获取场地编码
        String  zoneCode=wmMdWarehouseManage.getManagerZoneCode();//获取分区编码
        String  ManagerCode=siteCode+zoneCode;//生成煤场与分区的合成，作为煤场与分区的合并值

        // 根据siteCode查询WmMdWarehouse对象
        WmMdWarehouse wmMdWarehouse=wmMdWarehouseMapper.selectOne(new LambdaQueryWrapper<WmMdWarehouse>().eq(WmMdWarehouse::getSiteCode, siteCode));
        // 根据zoneCode查询WmMdZone对象
        WmMdZone wmMdZone=wmMdZoneMapper.selectOne(new LambdaQueryWrapper<WmMdZone>().eq(WmMdZone::getZoneCode, zoneCode));
        // 初始化managerName为空字符串 生成煤场与分区的合成，作为煤场与分区的合并值
        String  managerName="";
        // 如果wmMdWarehouse和wmMdZone都不为空，则将wmMdWarehouse的siteName和wmMdZone的zoneName拼接成managerName
        if(ObjectUtil.isNotEmpty(wmMdWarehouse) && ObjectUtil.isNotEmpty(wmMdZone)){
            managerName= wmMdWarehouse.getSiteName()+wmMdZone.getZoneName();
        }

        // 设置wmMdWarehouseManage的managerCode为ManagerCode
        wmMdWarehouseManage.setManagerCode(ManagerCode);
        // 设置wmMdWarehouseManage的managerName为managerName
        wmMdWarehouseManage.setManagerName(managerName);

        this.save(wmMdWarehouseManage);
    }

    @Override
    public void del(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        WmMdWarehouseManage wmMdWarehouseManage = this.queryWmMdWarehouseManage(wmMdWarehouseManageRequest);
        this.removeById(wmMdWarehouseManage.getManagerId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        this.removeByIds(wmMdWarehouseManageRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        WmMdWarehouseManage wmMdWarehouseManage = this.queryWmMdWarehouseManage(wmMdWarehouseManageRequest);
        BeanUtil.copyProperties(wmMdWarehouseManageRequest, wmMdWarehouseManage);
        this.updateById(wmMdWarehouseManage);
    }

    @Override
    public WmMdWarehouseManage detail(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        return this.queryWmMdWarehouseManage(wmMdWarehouseManageRequest);
    }

    @Override
    public PageResult<WmMdWarehouseManage> findPage(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        LambdaQueryWrapper<WmMdWarehouseManage> wrapper = createWrapper(wmMdWarehouseManageRequest);
        Page<WmMdWarehouseManage> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<WmMdWarehouseManage> findList(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        LambdaQueryWrapper<WmMdWarehouseManage> wrapper = this.createWrapper(wmMdWarehouseManageRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private WmMdWarehouseManage queryWmMdWarehouseManage(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        WmMdWarehouseManage wmMdWarehouseManage = this.getById(wmMdWarehouseManageRequest.getManagerId());
        if (ObjectUtil.isEmpty(wmMdWarehouseManage)) {
            throw new ServiceException(WmMdWarehouseManageExceptionEnum.WM_MD_WAREHOUSE_MANAGE_NOT_EXISTED);
        }
        return wmMdWarehouseManage;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private LambdaQueryWrapper<WmMdWarehouseManage> createWrapper(WmMdWarehouseManageRequest wmMdWarehouseManageRequest) {
        LambdaQueryWrapper<WmMdWarehouseManage> queryWrapper = new LambdaQueryWrapper<>();

        Long managerId = wmMdWarehouseManageRequest.getManagerId();
        queryWrapper.eq(ObjectUtil.isNotNull(managerId), WmMdWarehouseManage::getManagerId, managerId);

        String managerName = wmMdWarehouseManageRequest.getManagerName();
        queryWrapper.like(ObjectUtil.isNotEmpty(managerName), WmMdWarehouseManage::getManagerName, managerName);

        String managerCode = wmMdWarehouseManageRequest.getManagerCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(managerCode), WmMdWarehouseManage::getManagerCode, managerCode);

        String managerSiteCode = wmMdWarehouseManageRequest.getManagerSiteCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(managerSiteCode), WmMdWarehouseManage::getManagerSiteCode, managerSiteCode);

        String managerZoneCode = wmMdWarehouseManageRequest.getManagerZoneCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(managerZoneCode), WmMdWarehouseManage::getManagerZoneCode, managerZoneCode);

        BigDecimal managerZoneIdealCapacity = wmMdWarehouseManageRequest.getManagerZoneIdealCapacity();
        queryWrapper.eq(ObjectUtil.isNotNull(managerZoneIdealCapacity), WmMdWarehouseManage::getManagerZoneIdealCapacity, managerZoneIdealCapacity);

        BigDecimal managerZoneLowStock = wmMdWarehouseManageRequest.getManagerZoneLowStock();
        queryWrapper.eq(ObjectUtil.isNotNull(managerZoneLowStock), WmMdWarehouseManage::getManagerZoneLowStock, managerZoneLowStock);

        String remark = wmMdWarehouseManageRequest.getRemark();
        queryWrapper.like(ObjectUtil.isNotEmpty(remark), WmMdWarehouseManage::getRemark, remark);


        return queryWrapper;
    }

}
